package com.gph.etrip;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.gph.etrip.routehandler.NewGeoPoint;
import com.gph.etrip.routehandler.Road;

public class MapOverlay extends com.google.android.maps.Overlay {
	Road mRoad;
	ArrayList<GeoPoint> mPoints;
	ArrayList<NewGeoPoint> mMarkers;
	Context context;
	Rect rect;

	public MapOverlay(Context context, Road road, MapView mv, Rect rect) {
		this.context = context;
		this.rect = rect;
		mRoad = road;
		if (road.mRoute.length > 0) {
			mPoints = new ArrayList<GeoPoint>();
			mMarkers = new ArrayList<NewGeoPoint>();
			for (int i = 0; i < road.mRoute.length; i++) {
				mPoints.add((GeoPoint) new GeoPoint((int) (road.mRoute[i][1] * 1000000),
						(int) (road.mRoute[i][0] * 1000000)));
			}
			for (int i=0; i<road.mPoints.length-1; i++){
				mMarkers.add((NewGeoPoint) new NewGeoPoint((int)(road.mPoints[i].mLatitude*1000000),
						(int)(road.mPoints[i].mLongitude*1000000),road.mPoints[i].mIconUrl));
			}
			int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
					mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
					.getLatitudeE6()) / 2);
			int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
					mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
					.getLongitudeE6()) / 2);
			GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

			MapController mapController = mv.getController();
			mapController.animateTo(moveTo);
			mapController.setZoom(18);
		}
		//for debugging
		System.out.println("mRoad.mRoute"+mRoad.mRoute.length);
		System.out.println("mRoad.mPoints"+mRoad.mPoints.length);
		System.out.println("mPoints"+mPoints.size());
		System.out.println("mMarkers"+mMarkers.size());
		for(int i=0;i<mMarkers.size();i++){
			System.out.println("URL"+mMarkers.get(i).iconURL);
		}
		for(int i=0;i<mRoad.mPoints.length;i++){
			System.out.println("URL"+mRoad.mPoints[i].mIconUrl);
		}
	}

	@Override
	public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
		super.draw(canvas, mv, shadow);
		drawPath(mv, canvas);
		return true;
	}

	public void drawPath(MapView mv, Canvas canvas) {
		int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
		Paint paint = new Paint();
		paint.setColor(Color.GREEN);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(5);
  
		for(int i=0; i<mMarkers.size(); i++){
			Point point = new Point();
			mv.getProjection().toPixels(mMarkers.get(i), point);
			
			if(rect.contains(point.x, point.y)){
//			canvas.drawBitmap(getBitmapFromURL(mMarkers.get(i).iconURL), point.x, point.y-50, paint);
			Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.pushpin);
			canvas.drawBitmap(bmp, point.x, point.y-50, paint);
			}
		}
		
		for (int i = 0; i < mPoints.size(); i++) {
			Point point = new Point();
			mv.getProjection().toPixels(mPoints.get(i), point);
			x2 = point.x;
			y2 = point.y;
			if (i > 0) {
				if(rect.contains(point.x, point.y)){
				canvas.drawLine(x1, y1, x2, y2, paint);
				}
			}
			x1 = x2;
			y1 = y2;
		}
	}
	
	private static Bitmap getBitmapFromURL(String src) {
	    try {
	        URL url = new URL(src);
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoInput(true);
	        connection.connect();
	        InputStream input = connection.getInputStream();
	        Bitmap myBitmap = BitmapFactory.decodeStream(input);
	        return myBitmap;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    }
	}
}